using System.Collections.Generic;
using System.Text.RegularExpressions;
using UnityEngine;
using UnityEngine.UI;
using Voodoo.Sauce.Internal.Extension;
using Voodoo.Sauce.Internal.VoodooTune;

namespace Voodoo.Sauce.Debugger
{
	public class VoodooTuneConfigurationDetailsScreen : Screen
	{
		[Header("Configuration Details")]
		[SerializeField]
		private Text configurationDetailsLabel;

		[SerializeField]
		private Button copyButton;

		private static readonly Regex ClassNameRegex = new Regex("\\.([^\\.]*)$");

		private void OnEnable()
		{
			copyButton.onClick.AddListener(CopyConfigurationDetails);
		}

		private void OnDisable()
		{
			copyButton.onClick.RemoveListener(CopyConfigurationDetails);
		}

		private void Start()
		{
			configurationDetailsLabel.text = "";
			foreach (KeyValuePair<string, string> item in VoodooTuneManager.GetItemsJson())
			{
				Text text = configurationDetailsLabel;
				text.text = text.text + FormatClassName(item.Key) + FormatVariables(item.Value) + "\n";
			}
		}

		private static string FormatClassName(string value)
		{
			string text = ClassNameRegex.Match(value).Groups[1].Value;
			string text2 = "";
			if (string.IsNullOrEmpty(text))
			{
				text = value;
			}
			else
			{
				string text3 = value.Substring(0, value.Length - text.Length - 1);
				text2 = "<i><size=35><color=#4D4D4D>" + text3 + "</color></size></i>\n";
			}
			return "\n<b><size=45>" + text + "</size></b>\n" + text2 + "\n";
		}

		private static string FormatVariables(string value)
		{
			value = "\t" + value.Replace("[", "").Replace("]", "").Replace("{", "")
				.Replace("}", "")
				.Replace("\"", "")
				.Replace(",", "\n\t");
			value = Regex.Replace(value, "(.*):(.*)", "$1: $2");
			value = Regex.Replace(value, "([a-z])([A-Z])", "$1 $2");
			return value;
		}

		public void CopyConfigurationDetails()
		{
			VoodooTuneManager.GetRawConfiguration().CopyToClipboard();
		}
	}
}
